import pymysql

try:
    db = pymysql.connect(host='localhost', port=3306, user='root', passwd='liqian', db='math_score')
    cur = db.cursor()
    
    print("执行查询")
    cur.execute("insert into delbanji(classTeacher,grade) values('张三','2'),('李四','6')")  # 执行查询
    db.commit()
    print("提交成功")
except Exception as e:
    db.rollback()
    print(f"发生错误: {e}")  # 打印异常信息

finally:
    # 确保关闭游标和连接，即使发生了异常
    if cur:
        cur.close()
    if db:
        db.close()